package com.nju.edu.erp.web.controller;

import com.nju.edu.erp.auth.Authorized;
import com.nju.edu.erp.enums.Role;
import com.nju.edu.erp.enums.sheetState.PaySheetState;
import com.nju.edu.erp.model.vo.UserVO;
import com.nju.edu.erp.model.vo.pay.PaySheetVO;
import com.nju.edu.erp.service.finance.FinancePayService;
import com.nju.edu.erp.web.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(path = "/finance-pay")
public class  FinancePayController {
    private final FinancePayService financePayService;

    @Autowired
    public FinancePayController(FinancePayService financePayService) {
        this.financePayService = financePayService;
    }

    @Authorized(roles = {Role.FINANCIAL_STAFF, Role.GM, Role.ADMIN})
    @PostMapping(value = "/sheet-make")
    public Response makePayOrder(UserVO userVO, @RequestBody PaySheetVO paySheetVO) {
        System.out.println(paySheetVO);
        financePayService.makePaySheet(userVO, paySheetVO);
        return Response.buildSuccess();
    }

    @GetMapping(value = "/sheet-show")
    public Response showSheetByState(@RequestParam(value = "state", required = false) PaySheetState state)  {
        return Response.buildSuccess(financePayService.getPaySheetByState(state));
    }

//    @GetMapping(value = "/find-sheet")
//    public Response findBySheetId(@RequestParam(value = "id") String id)  {
//        return Response.buildSuccess(financePayService.getPaySheetById(id));
//    }

    @Authorized (roles = {Role.GM, Role.ADMIN})
    @GetMapping(value = "/approval")
    public Response Approval(@RequestParam("paySheetId") String paySheetId,
                             @RequestParam("state") PaySheetState state)  {
        if(state.equals(PaySheetState.FAILURE) || state.equals(PaySheetState.SUCCESS)) {
            financePayService.approval(paySheetId, state);
            return Response.buildSuccess();
        } else {
            return Response.buildFailed("000000","操作失败"); // code可能得改一个其他的
        }
    }
}
